/*
	Copyright (C) 2011 Daniel C. Dennison (dandennison84@gmail.com).  All rights reserved.
	This file is licensed under the EPL 1.0 (Eclipse Public License 1.0)
	A copy of the license can be found at http://www.eclipse.org/legal/epl-v10.html
*/

package com.gmail.dandennison84.accounting

import org.junit.Before
import org.junit.Test

class IncomeStatementTest {
	List<Account> coa

	@Before
	void before() {
		coa = new ChartOfAccountsHelper().chartOfAccounts
	}

	@Test
	void testAccountType() {
		StatementBuilder isb = new StatementBuilder(coa)
		isb.incomeStatement {
			revenue(accountType:AccountType.REVENUE)
		}
		Map<String, List<Account>> incomeStatement = isb.accounts
		assert 1 == incomeStatement.size()

		assert 4  == incomeStatement['revenue']?.size()		
		assert [findAccount(coa, 'Freight Revenues'), findAccount(coa, 'Other Revenues'),
			findAccount(coa, 'Cost of Sales'), findAccount(coa, 'Interest Income')] == incomeStatement['revenue']
	}

	@Test
	void testGroup() {
		StatementBuilder isb = new StatementBuilder(coa)
		isb.incomeStatement {
			revenue(accountGroup:'operatingRevenue')
		}
		Map<String, List<Account>> incomeStatement = isb.accounts
		assert 1 == incomeStatement.size()
		assert 3  == incomeStatement['revenue']?.size()		
		assert [findAccount(coa, 'Freight Revenues'), findAccount(coa, 'Other Revenues'),
			findAccount(coa, 'Cost of Sales')] == incomeStatement['revenue']
	}
	
	@Test
	void testAccount() {
		StatementBuilder isb = new StatementBuilder(coa)
		isb.incomeStatement {
			revenue {
				account 'Deliveries'
				account 'Sales'
				account 'Cost of Sales'
			}
		}
		Map<String, List<Account>> incomeStatement = isb.accounts
		assert 1 == incomeStatement.size()
		assert 3  == incomeStatement['revenue']?.size()		
		assert [findAccount(coa, 'Deliveries'), findAccount(coa, 'Sales'),
			findAccount(coa, 'Cost of Sales')] == incomeStatement['revenue']
	}

	@Test
	void testIncomeStatement() {
		JournalHelper jh = new JournalHelper()
		Map<String, List<Account>> is = jh.getIncomeStatement(coa)

		assert 3 == is['operatingRevenue'].size()
		assert 1 == is['nonOperatingRevenue'].size()
		assert 10 == jh.getNetIncome(is)
	}

	private Account findAccount(List<Account> coa, String name) {
		coa.find {it.name == name}
	}
}
